﻿{% extends "cmdb/mequipment/index.html" %}

{% block right_content %}
<div id="right_menu">
    <b class="modal-title">网络设备添加</b>
    <hr/>
    
    <form class="form-horizontal">
        
        <div class="form-inline">
            <label for="equipmentModel">设备型号</label>
            <input class="form-control" style="width:400px;" type="text" id="equipmentModel" placeholder="设备型号" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline">
            <label for="eqType">设备类型</label>
            <input class="form-control" style="width:400px;" type="text" id="eqType" placeholder="设备类型" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline">
            <label for="version">设备版本</label>
            <input class="form-control" style="width:400px;" type="text" id="version" placeholder="设备版本" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline" style="padding-left:12px">
            <label for="hostname" >主机名</label>
            <input class="form-control" style="width:400px;" type="text" id="hostname" placeholder="主机名" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline" style="padding-left:12px">
            <label for="manageIp">管理ip</label>
            <input class="form-control" style="width:400px;" type="text" id="manageIp" placeholder="管理ip" />
        </div><br />
        <div class="form-inline">
            <label for="assetNumber">资产编号</label>
            <input class="form-control" style="width:400px;" type="text" id="assetNumber" placeholder="资产编号" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline" style="padding-left:27px">
            <label for="serverRoom">机房</label>
            <select class="form-control" id="serverRoom" style="width: 400px;">
                   {% if server_room_list %}
                    {% for serverRoom in server_room_list %}
                        <option value="{{ serverRoom.id }}">{{ serverRoom.server_room }}</option>
                    {% endfor %}
                   {% endif %}
                </select>
            <span style="display: inline" id="warningRoomId"></span>
        </div><br />
        <div class="form-inline" style="padding-left:27px">
            <label for="cabinet">机柜</label>
            <input class="form-control" style="width:400px;" type="text" id="cabinet" placeholder="机柜" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline">
            <label for="uStartPos">机柜起位</label>
            <input class="form-control" style="width:400px;" type="text" id="uStartPos" placeholder="机柜起位" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline">
            <label for="uEndPos">机柜止位</label>
            <input class="form-control" style="width:400px;" type="text" id="uEndPos" placeholder="机柜止位" />
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline" style="padding-left:12px">
            <label for="seriesNo">序列号</label>
            <input class="form-control" style="width:400px;" type="text" id="seriesNo" placeholder="序列号" onchange="serialNoCheck()" />
            <span style="display: inline" id="warningSerialNo"></span>
        </div><br />
        <div class="form-inline">
            <label for="purchaseTime">采购日期</label>
            <input class="form-control" style="width:400px;" type="text" id="purchaseTime" placeholder="采购日期" readonly/>
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline">
            <label for="maintEndTime">维保到期</label>
            <input class="form-control" style="width:400px;" type="text" id="maintEndTime" placeholder="维保到期" readonly/>
            <span style="display:none;">*必填</span>
        </div><br />
        <div class="form-inline" style="padding-left:27px">
            <label for="comment">备注</label>
            <input class="form-control" style="width:400px;" type="text" id="comment" placeholder="备注" />
            <span style="display:none;">*必填</span>
        </div>
        <input type="text" id="operator" value="{{ request.session.username }}" style="display: none;">
    </form>
    <div style="padding-left: 100px;">
        <button type="button" class="btn btn-primary" id="item_add">
            提交
        </button>&nbsp;&nbsp;&nbsp;
        <button type="button" class="btn btn-default" id="add_cancel">
            取消
        </button>
    </div>
</div>

<script type="text/javascript">
var myDate1 = new Calender({id:'purchaseTime',isSelect:!0});
var myDate2 = new Calender({id:'maintEndTime',isSelect:!0});

function uniqueCheck(value,attr){
    var checkStatus = true;
    if(value == '' || value == '无'){
	    $("#warningSerialNo").html("");
	    return checkStatus;
    }
    var urls = "{% url 'equipment:netuniquecheck' %}";
    var postdata = {serial_num:value};
    var jsonstr = $.ajax({
        url : urls,
		type : "POST",
		data : postdata,
		async : false,
		cache : false
	});
	var jsonobj = eval('(' + jsonstr.responseText + ')');
	if(jsonobj.status == 1){
	    if(jsonobj.data == true){
	        $("#warningSerialNo").html("<font size='2' style='color:red'>设备已存在</font>");
	        checkStatus = false;
	    }else{
	        $("#warningSerialNo").html("");
	    }
	}else{
	    checkStatus = false;
	}
	return checkStatus;
}

//serial number unique check
function serialNoCheck(){
    var seriesNo = $("#seriesNo").val();
    uniqueCheck(seriesNo,'serial_num');
}

$("#item_add").click(function(){
    var checkStatus = true;
    var equipmentModel = $("#equipmentModel").val();
    var eqType = $("#eqType").val();
    var version = $("#version").val();
    var hostname = $("#hostname").val();
    var manageIp = $("#manageIp").val();
    var seriesNo = $("#seriesNo").val();
    if(!uniqueCheck(seriesNo,'serial_num')){
        checkStatus = false;
    }
    var assetNumber = $("#assetNumber").val();
    var serverRoom = $("#serverRoom").val();
    if(!serverRoom || serverRoom == ''){
        $("#warningRoomId").html("<font size='2' style='color:red'>机房为空</font>");
        checkStatus = false;
    }else{
        $("#warningRoomId").html("");
    }
    if(!checkStatus){
        return;
    }
    var cabinet = $("#cabinet").val();
    var uStartPos = $("#uStartPos").val();
    uStartPos = (uStartPos == '') ? '0' : uStartPos;
    var uEndPos = $("#uEndPos").val();
    uEndPos = (uEndPos == '') ? '0' : uEndPos;

    var purchaseTime = $("#purchaseTime").val();
    var maintEndTime = $("#maintEndTime").val();
    var comment = $("#comment").val();
    var operator = $("#operator").val();

    var urls = "{% url 'equipment:netaddaction' %}";
    var postdata = {equipmentModel:equipmentModel, eqType:eqType, version:version, hostname:hostname, manageIp:manageIp,
                    asset_number:assetNumber,serverRoom:serverRoom, cabinet:cabinet, uStartPos:uStartPos, uEndPos:uEndPos, seriesNo:seriesNo,
                    purchaseTime:purchaseTime, maintEndTime:maintEndTime, comment:comment, operator:operator}
    var jsonstr = $.ajax({
			url : urls,
			type : "POST",
			data : postdata,
			async : false,
			cache : false
		});
		var jsonobj = eval('(' + jsonstr.responseText + ')');
		if(jsonobj.status==0){
		    alert(jsonobj.message);
		}else{
            window.location.href="{% url 'equipment:netinfo' %}";
        }
});

$("#add_cancel").click(function(){
    window.location.href="{% url 'equipment:netinfo' %}";
});
</script>
{% endblock %}